package com.tanglongan.designPattern.dp08_composite.demo01;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程包
 */
public class CoursePackage extends CourseCompoent {

    private List<CourseCompoent> items = new ArrayList<>();
    private String name;
    private Integer level;

    public CoursePackage(String name, int level) {
        this.name = name;
        this.level = level;
    }

    @Override
    public void addChild(CourseCompoent compoent) {
        items.add(compoent);
    }

    @Override
    public void removeChild(CourseCompoent compoent) {
        items.remove(compoent);
    }

    @Override
    public String getName(CourseCompoent compoent) {
        return this.name;
    }

    @Override
    public void print() {
        System.out.println(this.name);
        for (CourseCompoent item : items) {
            if (this.level != null) {
                for (int i = 0; i < this.level; i++) {
                    System.out.print(" ");        //打印空格来控制显示格式
                }
            }

            for (int i = 0; i < this.level; i++) {
                if (i == 0) System.out.print("+");//每行开始打印一个+号
                System.out.print("-");
            }
            //打印标题
            item.print();
        }
    }

}
